package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.bean.ApiResult;
import com.example.mapper.CommentMapper;
import com.example.modules.dto.CommentDTO;
import com.example.modules.entity.Comment;
import com.example.modules.vo.CommentVO;
import com.example.service.CommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Override
    public ApiResult insertComment(CommentDTO commentDTO) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        comment.setCommentTime(new Date());
        save(comment);
        return ApiResult.success();
    }

    @Override
    public ApiResult datailComment(Integer postId,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        //查询一级评论
        QueryWrapper<Comment> qw = new QueryWrapper<>();
//        qw.eq("post_id",postId).eq("parent_id",0).eq("is_delete",0).orderByDesc("comment_time");
        qw.eq("post_id",postId).eq("is_delete",0).orderByDesc("comment_time");
        List<CommentVO> commentVOList = list(qw).stream().map(CommentVO::of).collect(Collectors.toList());
//        Map<Integer, List<CommentVO>> commentVoMap = commentVOList.stream().collect(Collectors.groupingBy(CommentVO::getParentId));
//        //一级评论
//        List<CommentVO> commentVOS = commentVoMap.get(0);
//        for (CommentVO commentVO : commentVOS) {
//            //二级评论
//            commentVO.setCommentVOList(commentVoMap.get(commentVO.getId()));
//        }
//        //查询二级评论信息
//        commentVOList.forEach(item->{
//            qw.clear();
//            qw.eq("parent_id",item.getId()).eq("post_id",postId)
//              .eq("is_delete",0).orderByDesc("comment_time");
//            item.setCommentVOList(list(qw).stream().map(CommentVO::of).collect(Collectors.toList()));
//        });
        List<CommentVO> commentVOS = setCommentChild(commentVOList);
        PageInfo<CommentVO> pageInfo = new PageInfo<>(commentVOS);
        return ApiResult.success(pageInfo);
    }

    public List<CommentVO> setCommentChild(List<CommentVO> commentVOList){
        List<CommentVO> root = new ArrayList<>();
        Map<Integer, CommentVO> map = commentVOList.stream().collect(Collectors.toMap(CommentVO::getId, Function.identity()));
        for (CommentVO commentVO : commentVOList) {
            // 如果是父菜单的话
            if (0 == commentVO.getParentId()){
                root.add(commentVO);
            }else {
                CommentVO commentVO1 = map.get(commentVO.getParentId());
                if (commentVO1!=null){
                    if (commentVO1.getCommentVOList() == null){
                        commentVO1.setCommentVOList(new ArrayList<>());
                    }
                    commentVO1.getCommentVOList().add(commentVO);
                }

            }
        }
        return root;
    }

    @Override
    public ApiResult deleteCommentById(Integer id) {
        Comment comment = getById(id);
        if (Objects.nonNull(comment)){
            comment.setIsDelete(1);
            updateById(comment);
            //删除该评论下的所有二级评论
            commentMapper.delCommentByPid(id);
        }
        return ApiResult.success();
    }

    @Override
    public ApiResult getMyPostCommentTotal(Integer userId) {
        return ApiResult.success(commentMapper.getMyPostCommentTotal(userId));
    }

    @Override
    public ApiResult getReplyMyComments(Integer userId) {
        return ApiResult.success(commentMapper.getReplyMyComments(userId));
    }
}
